import { Injectable } from '@angular/core';
import { BaseRequestOptions,Http,Response, HTTP_PROVIDERS,Headers,URLSearchParams} from '@angular/http';
import { GlobalService } from './global.service';
import { Group } from '../model/group.model';
import { BaseService } from './base.service';

@Injectable()
export class GroupService extends BaseService
{
  headers:Headers;
  constructor(private http:Http,private globalService:GlobalService) {
    super();
  }

  add(group:Group)
  {
    this.headers.set("x-token",this.globalService.getUser().token);
    return this.http.post(BaseService.apiUrl +'/groups',JSON.stringify(group),{headers:this.headers});
  }

  update(group:Group)
  {
    this.headers.set("x-token",this.globalService.getUser().token);
    return this.http.put(BaseService.apiUrl +'/groups/'+group._id,JSON.stringify(group),{headers:this.headers});
  }

  delete(groupId:string)
  {
    this.headers.set("x-token",this.globalService.getUser().token);
    return this.http.delete(BaseService.apiUrl +'/groups/'+groupId,{headers:this.headers});
  }

  get(groupId:string)
  {
    var options = new BaseRequestOptions();
    var params = new URLSearchParams();
    params.append("groupId",groupId);
    options.search = params;
    return this.http.get(BaseService.apiUrl+'/groups/'+groupId,options);
  }

  getGroups(projId:string)
  {
    if(this.globalService.getUser())
    {
      this.headers.set("x-token",this.globalService.getUser().token);
      return this.http.get(BaseService.apiUrl+'/groups/project/'+projId,{headers:this.headers});
    }
  }
}
